<?php
class controller extends spController
{
	//http检测控制模块
	
	function __construct(){ // 公用
		parent::__construct(); // 这是必须的
		$this->tpl_title = "产品模块|主题部分";
	}
	
	
	function start_detection()
	{
		//这里是开启检测功能
		$this->detection();
		//$this->moniter();
	}
	
	function stop_detection()
	{
		//这里是停止检测功能
	}
	
    /**
    +----------------------------------------------------------
    * 循环
    +----------------------------------------------------------
    */
    
	private function moniter()
    {    	
    	ignore_user_abort();
        set_time_limit(0);
        $interval=100;
        do
        {
			$this->detection();
			sleep($interval); 
		}while(true);
      }
      
      function start()
      {
      	//接收前台指令，控制系统
      	$url = "系统开启成功";
      	$id = $this->spArgs('id');
      	$runtime = spClass('runtime');
      	$t_time = $runtime->s_time($id);
      	$y_time = date("Y-m-d H:i:s");
      	$sb=strtotime($y_time)-$t_time['0']['runtime'];
      	if($sb<120)
      	{
      		$this->error("系统已开启，请不要重复开启", spUrl("main","index"));
      	}
      	else
      	{
      		switch ($id)
      		{
      			case 1:
      				$time = 60;
      				$path="http://www.speed-1.com/produce.php?c=CThttp&a=start_detection"."&time=".$time;
      				if($this->ffp($path))
      				{
      					$this->success("一分钟检测开启成功", spUrl("main","index"));
      				}
      				break;
      			case 2:
      				$time = 120;
      				$path="produce.php?c=CThttp&a=start_detection"."&time=".$time;
      				if($this->ffp($path))
      				{
      					$this->success("二分钟检测开启成功", spUrl("main","index"));
      				}
      				break;
      			case 5:
      				$time = 300;
      				$path="produce.php?c=CThttp&a=start_detection"."&time=".$time;
      				if($this->ffp($path))
      				{
      					$this->success("五分钟检测开启成功", spUrl("main","index"));
      				}
      				break;
      			case 10:
      				$time = 600;
      				$path="produce.php?c=CThttp&a=start_detection"."&time=".$time;
      				if($this->ffp($path))
      				{
      					$this->success("十分钟检测开启成功", spUrl("main","index"));
      				}
      				break;
      			case 15:
      				$time = 900;
      				$path="produce.php?c=CThttp&a=start_detection"."&time=".$time;
      				if($this->ffp($path))
      				{
      					$this->success("十五分钟检测开启成功", spUrl("main","index"));
      				}
      				break;
      			case 20:
      				$time = 1200;
      				$path="produce.php?c=CThttp&a=start_detection"."&time=".$time;
      				if($this->ffp($path))
      				{
      					$this->success("二十分钟检测开启成功", spUrl("main","index"));
      				}
      				break;
      			case 30:
      				$time = 1800;
      				$path="produce.php?c=CThttp&a=start_detection"."&time=".$time;
      				if($this->ffp($path))
      				{
      					$this->success("三十分钟检测开启成功", spUrl("main","index"));
      				}
      				break;
      			case 60:
      				$time = 3600;
      				$path="produce.php?c=CThttp&a=start_detection"."&time=".$time;
      				if($this->ffp($path))
      				{
      					$this->success("一小时检测开启成功", spUrl("main","index"));
      				}
      				break;
      		}
      	}

      }

    function triggerRequest($url, $post_data = array(), $cookie = array()) 
	{
		$method = "GET";
		dump($url);
		$url_array = parse_url ( $url );
		dump($url_array);
		$port = isset ( $url_array [port] ) ? $url_array [port] : 80;
		
		$fp = fsockopen ( $url_array [host], $port, $errno, $errstr, 30 );
		if (! $fp) {
			echo "$errstr ($errno)<br />\n";
			return FALSE;
		}
		$getPath = $url_array [path] . "?" . $url_array [query];
		if (! empty ( $post_data )) {
			$method = "POST";
		}
		$header = $method . " " . $getPath;
		$header .= " HTTP/1.1\r\n";
		$header .= "Host: " . $url_array [host] . "\r\n"; //HTTP 1.1 Host域不能省略

		/*以下头信息域可以省略
        $header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13 \r\n";
        $header .= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,q=0.5 \r\n";
        $header .= "Accept-Language: en-us,en;q=0.5 ";
        $header .= "Accept-Encoding: gzip,deflate\r\n";
         */
		$header .= "Connection: Close\r\n";
		
		
		if (! empty ( $cookie )) {
			$_cookie = strval ( NULL );
			foreach ( $cookie as $k => $v ) {
				$_cookie .= $k . "=" . $v . "; ";
			}
			$cookie_str = "Cookie: " . base64_encode ( $_cookie ) . " \r\n";
			$header .= $cookie_str;
		}
		if (! empty ( $post_data )) {
			$_post = strval ( NULL );
			foreach ( $post_data as $k => $v ) {
				$_post .= $k . "=" . $v . "&";
			}
			$post_str = "Content-Type: application/x-www-form-urlencoded\r\n"; 
			$post_str .= "Content-Length: " . strlen ( $_post ) . " \r\n"; 
			

			$post_str .= $_post . "\r\n\r\n"; 
			$header .= $post_str;
		}
		dump($header);
		fwrite ( $fp, $header );
		while (!feof($fp)) {
        	echo fgets($fp, 128);
    	}
		
		fclose ( $fp );
		return true;
	}     
    	
	function ffp($path) 
	{
		$system = spClass("system");
		
		$site = $system->findAll();
		$domain = $site[0]['domain'];
		$fp = fsockopen ( "$domain", 80, $errno, $errstr, 30 );
		if (! $fp) 
		{
			echo "$errstr ($errno)<br />\n";
		} else 
		{
			$header = "GET /$path HTTP/1.1\r\n";
			$header .= "Host:  $domain\r\n";
			$header .= "Connection: Close\r\n\r\n";
			dump($header);
			fwrite ( $fp, $header );
			fclose ( $fp );
			return true;
		}
	}

}